<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class Api_Model_LikeMapper {
	protected $_db_table;

	public function __construct() {
		$this->_db_table = new Api_Model_DbTable_Like();
	}

	public function like($data){

		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$currentDateTime = date('Y-m-d H-i-s');
		$like = array('location_id' => $data['location_id'],
                        'photo_id' => $data['photo_id'],
                        'user_id' => $data['user_id']);

		$affectedRow = $this->_db_table->insert($like);

		if ($affectedRow){
			$like_id = $this->_db_table->getAdapter()->lastInsertId();
			$this->_db_table->update(array('created_time' => $currentDateTime),
			array('like_id = ?' => $like_id));

		} else {
			throw new Exception('Internal Server Error' ,500);
		}
	}

	public function unLike($data, $user_id){
		if($data['photo_id'] == '' && $data['location_id'] != null){
			$where = array('user_id = ?' => $user_id, 'location_id = ?' => $data['location_id']);
			$this->_db_table->delete($where);
		} elseif ($data['photo_id'] != null && $data['location_id'] == ''){
			$where = array('user_id = ?' =>$user_id, 'photo_id = ?' => $data['photo_id']);
			$this->_db_table->delete($where);
		}
	}

}